package com.Sony.TemplateGame;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;


/**
 *	Class untuk mengatur player
 *	@author sony wirawan
 **/
public class Player 
{
	private TemplateGameActivity core;
	private SceneUserData scene_userdata;
	public  PlayerUserData player_userdata;
	
	private BitmapTextureAtlas mOnScreenControlTexture;
	public TextureRegion mOnScreenControlBaseTextureRegion;
	public TextureRegion mOnScreenControlKnobTextureRegion;
	public DigitalOnScreenControl mDigitalOnScreenControl;
	
	private enum PlayerDirection
	{	NONE,
		UP,
		DOWN,
		LEFT,
		RIGHT
	}
	private PlayerDirection playerDirection = PlayerDirection.NONE;
	
	public final long[] ANIMATE_DURATION = new long[]{200, 200, 200};
	public final int PLAYER_VELOCITY = 2;
	
	
	private BitmapTextureAtlas mTexturePlayer;
	private TiledTextureRegion mPlayerTextureRegion;	
	private Body mPlayerBody;
	public AnimatedSprite sprite;
	
	public enum Quest
	{	GET_BOX,
		GET_BADGE,
		GET_FACE
	}
	public Quest player_quest;
		
	
	/**
	 *	Constructor player
	 *	@param BA = context yang digunakan
	 *	@author sony wirawan
	 **/
	public Player(TemplateGameActivity template)
	{	this.core = template; }

	
	/**
	 *	Method untuk memuat controller texture
	 *	@author sony wirawan
	 **/
	public void loadControlResources() 
	{	// TODO Auto-generated method stub
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		// Control texture
		this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this.core, "onscreen_control_base.png", 0, 0);
		this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this.core, "onscreen_control_knob.png", 128, 0);
	
		// Load the textures
		this.core.getEngine().getTextureManager().loadTextures(this.mOnScreenControlTexture);
	}

	

	/**
	 *	Method untuk controller handling
	 *	@author sony wirawan
	 **/
	public void loadControl()
	{	// TODO Auto-generated method stub
		this.mDigitalOnScreenControl = new DigitalOnScreenControl
		(	0,
			this.core.CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(),
			this.core.mBoundChaseCamera,
			this.mOnScreenControlBaseTextureRegion, 
			this.mOnScreenControlKnobTextureRegion, 
			0.1f, 
			new IOnScreenControlListener() 
			{
				
				public void onControlChange
				(	final BaseOnScreenControl pBaseOnScreenControl,
					final float pValueX,
					final float pValueY
				) 
				{
					// Set the correct walking animation
					if (pValueY == 1)
					{	// Up
						if (playerDirection != PlayerDirection.UP)
						{	sprite.animate(ANIMATE_DURATION, 0, 2, true);
							playerDirection = PlayerDirection.UP;
						}
					}
					else if (pValueY == -1)
					{	// Down
						if (playerDirection != PlayerDirection.DOWN)
						{	sprite.animate(ANIMATE_DURATION, 9, 11, true);
							playerDirection = PlayerDirection.DOWN;
						}
					}
					else if (pValueX == -1)
					{	// Left
						if (playerDirection != PlayerDirection.LEFT)
						{	sprite.animate(ANIMATE_DURATION, 3, 5, true);
							playerDirection = PlayerDirection.LEFT;
						}
					}
					else if (pValueX == 1)
					{	// Right
						if (playerDirection != PlayerDirection.RIGHT)
						{	sprite.animate(ANIMATE_DURATION, 6, 8, true);
							playerDirection = PlayerDirection.RIGHT;
						}
					}
					else
					{	if (sprite.isAnimationRunning())
						{	sprite.stopAnimation();
							playerDirection = PlayerDirection.NONE;
						}
					}
					
					scene_userdata = (SceneUserData) core.getEngine().getScene().getUserData();
					scene_userdata.mPlayerBody.setLinearVelocity
					(	pValueX * core.player.PLAYER_VELOCITY,
						pValueY * core.player.PLAYER_VELOCITY
					);
				}
			}
		);
		
		this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
		this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
		this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
		this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
		this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
		this.mDigitalOnScreenControl.refreshControlKnobPosition();
	}


	/**
	 *	Method untuk memuat player texture
	 *	@author sony wirawan
	 **/
	public void loadPlayerResources() 
	{	// TODO Auto-generated method stub
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		// Player sprite texture
		this.mTexturePlayer = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this.core, "hero.png", 0, 0, 3, 4);
		
		// Load the textures
		this.core.getEngine().getTextureManager().loadTextures(this.mTexturePlayer);
		
		// Calculate the coordinates for the player, so it's centred on the camera.
		final int centerX = (core.CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
		final int centerY = (core.CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;

		// Create the player sprite and add it to the scene.
		this.sprite = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
		core.mBoundChaseCamera.setChaseEntity(this.sprite);
	}


	/**
	 *	Method untuk memuat player
	 *	@param mPhysicsWorld, implementasi dari physics library yang digunakan
	 *	@return, body dari player
	 *	@author sony wirawan
	 **/
	public Body loadPlayer(FixedStepPhysicsWorld mPhysicsWorld) 
	{	// TODO Auto-generated method stub
		// create player body
		final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
		this.mPlayerBody = PhysicsFactory.createBoxBody(mPhysicsWorld, this.sprite, BodyType.DynamicBody, playerFixtureDef);
		mPhysicsWorld.registerPhysicsConnector
		(	new PhysicsConnector
			(	this.sprite, this.mPlayerBody, true, false )
			{	@Override
				public void onUpdate(float pSecondsElapsed)
				{	super.onUpdate(pSecondsElapsed);
					core.mBoundChaseCamera.updateChaseEntity();
				}
			}
		);
		
		// set player attribute
		this.player_userdata = new PlayerUserData
		( 0, Quest.GET_BOX );
		this.sprite.setUserData(this.player_userdata);
		this.mPlayerBody.setUserData("player");
		
		return this.mPlayerBody;
	}
}
